package com.asset.salesforce.controller.pages;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asset.salesforce.domain.Company;
import com.asset.salesforce.domain.Country;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.JobTitle;
import com.asset.salesforce.domain.JoinRequest;
import com.asset.salesforce.domain.Role;
import com.asset.salesforce.domain.User;
import com.asset.salesforce.dto.EmployeeDTO;
import com.asset.salesforce.service.CompanyService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.JobTitleService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.RoleShiroService;
import com.asset.salesforce.service.UserShiroService;

// TODO: Auto-generated Javadoc
/**
 * The Class EmployeeController.
 */
@Controller
public class EmployeeController {
	
	/** The country service. */
	@Autowired
	private CountryService countryService;
	
	/** The company service. */
	@Autowired
	private CompanyService companyService;
	
	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/** The job title service. */
	@Autowired
	private JobTitleService jobTitleService;
	
	/** The join request service. */
	@Autowired
	private JoinRequestService joinRequestService;
	
	/** The user shiro service. */
	@Autowired
	private UserShiroService userShiroService;

	/** The role shiro service. */
	@Autowired
	private RoleShiroService roleShiroService;
	
	/**
	 * Gets the owner register page.
	 *
	 * @param model the model
	 * @return the owner register page
	 */
	@RequestMapping(value = "/ownerRegister",method=RequestMethod.GET)
	public String getOwnerRegisterPage(Model model) {
		List<Country>countries = countryService.getAll();
		model.addAttribute("countries",countries);
		model.addAttribute("employeeDTO",new EmployeeDTO());
	    return "ownerPage";  
	}
	
	/**
	 * Post new owner.
	 *
	 * @param session the session
	 * @param request the request
	 * @param response the response
	 * @param model the model
	 * @param employeeDTO the employee dto
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value="/ownerRegister" ,method=RequestMethod.POST)
	public String postNewOwner(HttpSession session,HttpServletRequest request,HttpServletResponse response,
			Model model,@Valid @ModelAttribute("employeeDTO") EmployeeDTO employeeDTO,BindingResult result){
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
				
		Employee employee = new Employee();
		Company company = new Company();
		
		company.setCompanyName(employeeDTO.getCompanyDTO().getCompanyName());
		company.setEmployeesNumber(employeeDTO.getCompanyDTO().getEmployeesNumber());
		companyService.add(company);
		
		Country empCountry = countryService.getById(employeeDTO.getCountryDTO().getId());
		employee.setCompany(company);
		employee.setNickName(employeeDTO.getNickName());
		employee.setEmail(employeeDTO.getEmail());
		employee.setfName(employeeDTO.getfName());
		employee.setIsActive(true);
		employee.setIsOwner(true);
		employee.setlName(employeeDTO.getlName());
		employee.setManager(null);
		employee.setDateCreated(new Date());
		employee.setFirstLogin(true);
		employee.setCountry(empCountry);
		employeeService.add(employee);
		
		User user = new User();
		user.setEmail(employee.getEmail());
		user.setPassword(new Sha256Hash(employeeDTO.getUserDTO().getPassword()).toHex());
		user.setUsername(employee.getNickName());
		user.setCompanyId(employee);
		//
		List<Role> roles = new ArrayList<Role>();
		roles = roleShiroService.getAllRoles();
		user.setRoles(roles);
		userShiroService.addUser(user);
		return  "redirect:thanks";
	}
	
	/**
	 * Gets the emp register page.
	 *
	 * @param model the model
	 * @return the emp register page
	 */
	@RequestMapping(value = "/empRegister",method=RequestMethod.GET)
	public String getEmpRegisterPage(Model model) {
		List<Country>countries = countryService.getAll();
		model.addAttribute("countries",countries);
		List<JobTitle>jobTitles = jobTitleService.getAll();
		model.addAttribute("jobTitles",jobTitles);
		model.addAttribute("employeeDTO",new EmployeeDTO());
	    return "empRegisterPage";  
	}
	
	/**
	 * Post new employee.
	 *
	 * @param session the session
	 * @param request the request
	 * @param response the response
	 * @param model the model
	 * @param employeeDTO the employee dto
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value="/empRegister" ,method=RequestMethod.POST)
	public String postNewEmployee(HttpSession session,HttpServletRequest request,HttpServletResponse response,
			Model model,@Valid @ModelAttribute("employeeDTO") EmployeeDTO employeeDTO,BindingResult result){
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		JobTitle jobTitleObj = jobTitleService.getById(employeeDTO.getJobTitleDTO().getId());
		Employee companyOwner = employeeService.getCompanyOwner(employeeDTO.getCompanyDTO().getCompanyName());
		Country country = countryService.getById(employeeDTO.getCountryDTO().getId());
		
		Employee employee = new Employee();
		employee.setEmail(employeeDTO.getEmail());
		employee.setfName(employeeDTO.getfName());
		employee.setlName(employeeDTO.getlName());
		employee.setJobTitle(jobTitleObj);
		employee.setIsActive(false);	
		employee.setNickName(employeeDTO.getNickName());
		employee.setIsOwner(false);
		employee.setFirstLogin(true);
		employee.setDateCreated(new Date());
		employee.setCompany(companyOwner.getCompany());
		employee.setCountry(country);
		employeeService.add(employee);
		
		JoinRequest joinRequest = new JoinRequest();
		joinRequest.setOwner(companyOwner);
		joinRequest.setEmp(employee);
		joinRequest.setStatus(0);
		joinRequest.setStartDate(new Date());
		joinRequestService.add(joinRequest);
		
		return  "redirect:empRegThanks";
	}
}
